(when (not (= (vector-length command-line-arguments) 3))
   (display "Usage: monos -f decompose.scm ( file | uri ) location-of-generators method\n")
   (exit))

(let* ((uri (if (equal? (vector-ref command-line-arguments 0) "file")
                (file->uri (vector-ref command-line-arguments 1))
                (vector-ref command-line-arguments 1) ) )
       (input-ideal (unmarshall (parse-xml-from-uri->document uri)) ) 
       (original-ideal (copy-monomial-ideal input-ideal deep: #t)) 
       (original-mtree (monomial-ideal->monomial-tree original-ideal))
       (decomposition (decompose-ideal input-ideal method: (string->symbol (vector-ref command-line-arguments 2))))
       (ideal (apply intersect (lexed-list->list decomposition))) 
       (ideal-mtree (monomial-ideal->monomial-tree ideal)) 
       (not-in-original '())
       (not-in-new '()))

      (display "<test>")
      (newline)
      (display "<input>")
      (newline)
      (write original-ideal)
      (display "</input>")
      (newline)
      (display "<output>")
      (newline)
      (write decomposition)
      (newline)
      (write ideal)
      (display "</output>")
      (newline)

      (lexed-list-for-each 
         (lambda (m :: <com.milowski.monos.monomial.Monomial>)
             (unless (contains? original-mtree m) 
                 (set! not-in-original (cons m not-in-original))))
         ideal)
      (lexed-list-for-each 
         (lambda (m :: <com.milowski.monos.monomial.Monomial>)
             (unless (contains? ideal-mtree m) 
                 (set! not-in-new (cons m not-in-new))))
         original-ideal)
      (if (and (null? not-in-original) (null? not-in-new))
         (display "<ok/>\n");
         (begin
           (display "<error>\n")
           
           (fluid-let ((*lex-order* (lex-order-ref original-ideal)))
               (unless (null? not-in-original)
                  (display "Not in original: ");
                  (for-each (lambda (m) (display (monomial->string m)) (display " ")) not-in-original)
                  (newline))
               (unless (null? not-in-new)
                  (display "Not in new: ");
                  (for-each (lambda (m) (display (monomial->string m)) (display " ")) not-in-new)
                  (newline)) )

           (display "</error>")
           (newline)) ) 
      (display "</test>")
      (newline) 
      (unless (and (null? not-in-original) (null? not-in-new))
          (error "Ideals not equal"))
)

